"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 主机在start状态下执行failover
Create At   : 2021/01/20
Owner       : opentestcase015
Description :
    1.在备机上执行failover（备机执行）
    2.执行refrashconf进行信息写入
    3.重启集群
    4.检查主备是否切换成功
    5.环境恢复，备机重建
Expect      :
    1.在备机上执行failover成功
    2.执行refrashconf进行信息写入失败
    3.重启集群成功
    4.检查主备状态，主备切换失败，备机状态为need repair
    5.集群状态正常，备机重建成功
History     :
    Modified by opentestcase027 2023/1/12: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
import time
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.StandbyNode = Node('Standby1DbUser')
        self.sh_standby = CommonSH('Standby1DbUser')

    def test_system_internal_tools(self):
        LOG.info('--------step1:failover备升主  expect: 成功--------')
        excute_msg = self.sh_standby.execute_gsctl(
            "failover", self.constant.FAILOVER_SUCCESS_MSG, param="-m fast")
        self.assertTrue(excute_msg)

        LOG.info('------step2:进行refreshconf信息写入  expect: 写入失败-------')
        excute_msg = self.sh_standby.exec_refresh_conf()
        self.assertFalse(excute_msg)

        LOG.info('-------step3:重启集群           expect: 成功--------')
        self.sh_primary.restart_db_cluster()
        status = self.sh_standby.get_db_cluster_status(param='detail')
        self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status)

        LOG.info('-----step4:查看主备状态切换     expect: 备机need repair------')
        status_msg = self.sh_standby.get_db_cluster_status(param='detail')
        LOG.info(status_msg)
        self.assertTrue(len(status_msg.splitlines()) >= 10)
        self.assertIn('Need repair', status_msg.splitlines()[-2].strip())

    def tearDown(self):
        LOG.info('--step5:环境恢复,备机重建    expect:集群状态正常，备机重建成功--')
        build_msg = self.sh_standby.build_standby(mode='')
        LOG.info(build_msg)
        time.sleep(3)
        recover_msg = self.sh_standby.get_db_cluster_status(param='detail')
        LOG.info(recover_msg)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, recover_msg)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')
